﻿package com.gui.scrollbar {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import gs.TweenMax;
	
	/**
	 * ...
	 * @author Ricardo Teixeira
	 */
	public class TextFieldScrollBar extends BasicScrollBar {
		
		private var campo:TextField;
		private var autoScroll:Boolean;
		private var arrastando:Boolean;
		
		public function TextFieldScrollBar (campo:TextField, autoScroll:Boolean = false) {
			this.campo = campo;
			this.autoScroll = autoScroll;
			this.arrastando = false;
			this.addEventListener (Event.ADDED_TO_STAGE, onInit);
		}
		
		public function resize (h:Number):void {
			this.drag.height = h;
			this.fundo.height = h;
		}
		
		private function onInit(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, onInit);
			
			
			this.campo.addEventListener (Event.SCROLL, onScroll);
			this.drag.addEventListener (MouseEvent.MOUSE_DOWN, onDragDown);
			this.drag.addEventListener (MouseEvent.MOUSE_UP, onDragUp);
			this.fundo.addEventListener (MouseEvent.MOUSE_DOWN, onDownFundo);
			
			this.addEventListener (Event.ENTER_FRAME, onUpdate);
		}
		
		private function onUpdate(e:Event):void {
			if (this.campo.maxScrollV > 1) {
				this.drag.height = this.fundo.height * 0.3;
			}
			if (this.autoScroll && !this.arrastando) {
				this.mover (this.fundo.height);
			}
		}
		
		public function mover (pos:Number):void {
			var perc:Number = pos / this.fundo.height * 100;
			var yDrag:Number = perc * (this.fundo.height - this.drag.height) / 100;
			this.drag.y = yDrag;
			
			perc = Math.round (this.drag.y / (this.fundo.height - this.drag.height) * 100);	
			this.campo.scrollV = Math.round (yDrag / (this.fundo.height - this.drag.height) * 100);	
		}
		
		private function onDownFundo(e:MouseEvent):void {
			this.mover (this.mouseY);
		}
		
		private function onScroll(e:Event):void {
			
			if (!this.arrastando) {				
				var perc:Number = Math.round (this.campo.scrollV / this.campo.maxScrollV * 100);
				this.drag.y = perc * (this.fundo.height - this.drag.height) / 100;
			}
		}
		
		private function onDragMove(e:MouseEvent):void {
			e.updateAfterEvent ();
			var limites:Rectangle = new Rectangle (this.drag.x, 0, 0, this.fundo.height - this.drag.height);
			this.drag.startDrag (false, limites);
			var perc:Number = Math.round (this.drag.y / (this.fundo.height - this.drag.height) * 100);			
			this.campo.scrollV = Math.round (perc * this.campo.maxScrollV / 100);
		}
		
		private function onDragUp(e:MouseEvent):void {
			this.arrastando = false;
			this.drag.removeEventListener (MouseEvent.MOUSE_MOVE, onDragMove);	
			this.stage.removeEventListener (MouseEvent.MOUSE_UP, onDragUp);	
			this.stage.removeEventListener (MouseEvent.MOUSE_MOVE, onDragMove);	
			this.drag.stopDrag ();
		}
		
		private function onDragDown(e:MouseEvent):void {
			this.arrastando = true;
			this.drag.addEventListener (MouseEvent.MOUSE_MOVE, onDragMove);	
			this.stage.addEventListener (MouseEvent.MOUSE_UP, onDragUp);	
			this.stage.addEventListener (MouseEvent.MOUSE_MOVE, onDragMove);	
			
		}
		
	}
	
}